﻿namespace A12_Ex01_1
{
    public class Program
    {
        private const int k_EndOfSequence = -1;

        public static void Main()
        {
            System.Console.WriteLine("Please enter integers. After each one press 'enter'. To Finish, enter '" + k_EndOfSequence.ToString() + "'.");
            
            int firstNumber = getNumberFromConsole();           
            int maxOfNumbers = firstNumber;         
            int minOfNumbers = firstNumber;
            float averageOfNumbers = firstNumber;
            int countOfDivisorsOfFirstNum = 0;                                    
            int currentNumber = firstNumber;
            int inputCount = 0;
            bool endOfSequence = firstNumber == k_EndOfSequence;       
            
            while (!endOfSequence)
            {
                maxOfNumbers = System.Math.Max(currentNumber, maxOfNumbers);
                minOfNumbers = System.Math.Min(currentNumber, minOfNumbers);
                bool isDivisor = (currentNumber != 0) && (firstNumber % currentNumber == 0) && (inputCount > 0);
                if (isDivisor)
                {
                    countOfDivisorsOfFirstNum++;
                }

                averageOfNumbers = ((averageOfNumbers * inputCount) + currentNumber) / (inputCount + 1);
                inputCount++; 
                currentNumber = getNumberFromConsole();
                endOfSequence = currentNumber == k_EndOfSequence;
            }

            bool emptyInputSequence = inputCount == 0;
            if (emptyInputSequence)
            {
                System.Console.WriteLine("No statistics to show, no numbers were entered. Goodbye!");
            }
            else
            {
                string statisticsMessage = string.Format( 
                                                           "Biggest number entered: {1}{0}Smallest number entered: {2}{0}Sequence size: {3}{0}Sequence average: {4}{0}Count of numbers that divide the first number, {5}: {6}",
                                                           System.Environment.NewLine,
                                                           maxOfNumbers, 
                                                           minOfNumbers, 
                                                           inputCount, 
                                                           averageOfNumbers, 
                                                           firstNumber, 
                                                           countOfDivisorsOfFirstNum);
                System.Console.WriteLine(statisticsMessage);
            }
        }

        private static int getNumberFromConsole()
        {
            int outputNum;

            string inputNumberStr = System.Console.ReadLine();
            while (!int.TryParse(inputNumberStr, out outputNum) || !(outputNum >= 0 || outputNum == k_EndOfSequence))
            {
                System.Console.WriteLine("This wasn't a valid integer, please try again:");
                inputNumberStr = System.Console.ReadLine();
            }

            return outputNum;          
        }
    }
}
